package com.lzy.cloud.ticket.train.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.ticket.train.component.MemberToken;
import com.lzy.cloud.ticket.train.dtos.TrainOrderDto;
import com.lzy.cloud.ticket.train.facade.TrainOrderFacade;
import com.lzy.cloud.ticket.train.request.app.*;
import com.lzy.cloud.ticket.train.response.TrainOrderDtoResponse;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 订单
 * Created by WIN7 on 2018/6/1.
 */
@RestController
@RequestMapping(value = "/order")
public class TrainOrderController extends BaseController {

    @Autowired
    private TrainOrderFacade trainOrderFacade;


    /**
     * 预订订单
     *
     * @return
     */
    @RequestMapping(value = "/booking", method = RequestMethod.POST)
    public Result bookingOrder(@RequestHeader("token") String token, @RequestBody OrderBookingAppRequest orderBookingInfoRequest) {
        MemberToken mt = isLogin(token);
//        Ensure.that(mt).isNotNull("00010011");
        Result result = trainOrderFacade.bookingOrder(mt.getId(),orderBookingInfoRequest);
        return result;
    }

    /**
     * 火车票抢票
     *
     * TODO 未测试
     */
    @RequestMapping(value = "/robbing", method = RequestMethod.POST)
    public Result ticketRobbing(@RequestHeader("token") String token,@RequestBody OrderBookingAppRequest orderBookingAppRequest) {
        MemberToken mt = isLogin(token);
//        Ensure.that(mt).isNotNull("00010011");
        Result result = trainOrderFacade.bookGrabOrder(mt.getId(),orderBookingAppRequest);
        return result;
    }

    /**
     * 取消待支付的订单
     *
     * @return
     */
    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    public Result cancelOrder(@RequestHeader("token") String token,@RequestBody OrderCancelAppRequest orderCancelAppRequest) {
        MemberToken mt = isLogin(token);
//        Ensure.that(mt).isNotNull("00010011");
        Result result = trainOrderFacade.cancelOrder(token,orderCancelAppRequest);
        return result;
    }


    /**
     * 确认出票
     *
     * @return
     *
     */
    @RequestMapping(value = "/confirm", method = RequestMethod.POST)
    public Result confirmTicket(@RequestHeader("token") String token,@RequestBody ConfirmTicketAppRequest confirmTicketAppRequest) {
        MemberToken mt = isLogin(token);
//        Ensure.that(mt).isNotNull("00010011");

        Result result = trainOrderFacade.confirmTicket(token,confirmTicketAppRequest);
        return result;
    }

    /**
     * 订单退票
     *
     * @return
     *
     */
    @RequestMapping(value = "/return", method = RequestMethod.POST)
    public Result returnTicket(@RequestHeader("token") String token,@RequestBody ReturnTicketAppRequest returnTicketAppRequest) {
        MemberToken mt = isLogin(token);
//        Ensure.that(mt).isNotNull("00010011");

        Result result = trainOrderFacade.returnTicket(token,returnTicketAppRequest);
        return result;
    }

    /**
     * 订单改签
     * TODO
     * @return
     * @TODO 未测试
     */
    @RequestMapping(value = "/change", method = RequestMethod.POST)
    public Result changeTicket(@RequestHeader("token") String token,@RequestBody ChangeOrderAppRequest changeOrderAppRequest) {
        MemberToken mt = isLogin(token);
//        Ensure.that(mt).isNotNull("00010011");

        Result result = trainOrderFacade.changeTicket(token,changeOrderAppRequest);
        return result;
    }

    /**
     * 订单详情
     *
     * @return
     *
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public Result queryOrderDetail(@RequestBody OrderDetailAppRequest orderDetailAppRequest) {
        Result result = trainOrderFacade.queryOrderDetail(orderDetailAppRequest);

        return result;
    }

    /**
     *  查询某会员本地火车票订单
     *
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result queryOrderList(@RequestHeader("token") String token,Pager pager){
        MemberToken mt = isLogin(token);
        Ensure.that(mt).isNotNull("00010001");

        PageInfo<TrainOrderDto> pageInfo = trainOrderFacade.queryOrderList(mt.getId(),pager);
        if (pageInfo != null && pageInfo.getList() != null) {
            pager.setTotal(pageInfo.getTotal());
            return new Result(pager,DozerUtils.maps(pageInfo.getList(), TrainOrderDtoResponse.class));
        } else {
            return new Result();
        }
    }

}
